package com.example.myapplication;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;

public class Reader_Activity extends AppCompatActivity {
    private ImageView img;
    private ListView listView;
    private ReaderDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_reader);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化视图
        img = findViewById(R.id.imageView3);
        listView = findViewById(R.id.listView); // 确保你的布局中有这个ListView

        // 初始化数据库帮助类
        dbHelper = new ReaderDatabaseHelper(this);

        // 加载读者数据
        loadReaders();

        // 返回按钮
        img.setOnClickListener(v -> {
            finish();
        });
    }

    private void loadReaders() {
        List<Reader> readerList = dbHelper.getAllReaders();
        ReaderAdapter adapter = new ReaderAdapter(this, readerList);
        listView.setAdapter(adapter);
    }
}